package com.woniuxy.dudugua.service.impl;

import com.woniuxy.dudugua.entity.*;
import com.woniuxy.dudugua.exception.IncorrectCredentialsException;
import com.woniuxy.dudugua.mapper.*;
import com.woniuxy.dudugua.service.MerchantsService;
import com.woniuxy.dudugua.util.view.MerchantsView;
import com.woniuxy.dudugua.util.view.ReturnGoodsView;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 实现商家业务层
 */
@Service
public class MerchantsServiceImpl implements MerchantsService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private LogisticsMapper logisticsMapper;
    @Resource
    private ReturnGoodsMapper returnGoodsMapper;
    @Resource
    private ReturnProcessMapper returnProcessMapper;
    @Resource
    private UserMapper userMapper;
    /**
     * 传入商家id，查询该先查询该用户的店铺id然后查询所有订单
     * @param id
     * @return
     * @throws Exception
     */
    public List<MerchantsView> selectMerchantsOrder(Integer id,Integer index,Integer number) throws Exception {
        Integer storeId = personMapper.selectByPrimaryKey(id).getStoreId();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("storeId",storeId);
        map.put("index",index);
        map.put("number",number);
        List<MerchantsView> merchantsViews = orderMapper.selectMerchantsOrder(map);
        return merchantsViews;
    }

    /**
     * 修改订单状态，增加运单号,增加物流信息
     * @param orderId
     * @param orderInfoId
     * @param courierNumber
     * @throws Exception
     */
    public void sendGoods(Integer orderId, Integer orderInfoId, String courierNumber,String companyCode) throws Exception {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        //查询orderinfo中orderid的信息，如果运单号都不为空，就更新order的状态
        OrderInfoExample example = new OrderInfoExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        List<OrderInfo> orderInfos = orderInfoMapper.selectByExample(example);
        for(OrderInfo orderInfo:orderInfos){
            if(orderInfo.getCourierNumber()==""||orderInfo.getCourierNumber()==null){
                //如果有没发货的，就不更新订单状态
                return;
            }
            order.setStatus(3);
            orderMapper.updateByPrimaryKey(order);
        }
        orderMapper.updateByPrimaryKey(order);
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderInfoId);
        orderInfo.setCourierNumber(courierNumber);
        orderInfoMapper.updateByPrimaryKey(orderInfo);
        Logistics logistics = new Logistics();
        logistics.setCourierNumber(courierNumber);
        logistics.setCompanyCode(companyCode);
        logistics.setStatus(1);
        logisticsMapper.insert(logistics);
    }

    /**
     * 修改returngoods状态为5，新增returnprocess表
     * @param returnGoodsId
     * @param userId
     * @throws Exception
     */
    public void commitReturn(Integer returnGoodsId, Integer userId) throws Exception {
        ReturnGoods returnGoods = returnGoodsMapper.selectByPrimaryKey(returnGoodsId);
        returnGoods.setStatus(5);
        returnGoodsMapper.updateByPrimaryKey(returnGoods);
        ReturnProcess returnProcess = new ReturnProcess();
        returnProcess.setReturnGoodsId(returnGoodsId);
        returnProcess.setUserId(userId);
        returnProcess.setStatus(1);
        returnProcessMapper.insert(returnProcess);
    }

    /**
     * 通过商家id，查询该商家的所有退款信息
     * @param userId
     * @return
     * @throws Exception
     */
    public List<ReturnGoodsView> selectAllReturnGoods(Integer userId,Integer index,Integer number) throws Exception {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("userId",userId);
        map.put("index",index);
        map.put("number",number);
        List<ReturnGoodsView> returnGoodsViews = returnGoodsMapper.selectAllReturnGoods(map);
        return returnGoodsViews;
    }

    /**
     * 商家拒绝退货
     * @param returnGoodsId
     * @throws Exception
     */
    public void rejectReturn(Integer returnGoodsId) throws Exception {
        ReturnGoods returnGoods = returnGoodsMapper.selectByPrimaryKey(returnGoodsId);
        returnGoods.setStatus(2);
        returnGoodsMapper.updateByPrimaryKey(returnGoods);
    }

    /**
     * 商家确认收货同时拒绝退款
     * @param returnGoodsId
     * @throws Exception
     */
    public void rejectAndAcceptGoods(Integer returnGoodsId) throws Exception {
        //修改returngoods中的状态为2
        ReturnGoods returnGoods = returnGoodsMapper.selectByPrimaryKey(returnGoodsId);
        returnGoods.setStatus(2);
        returnGoodsMapper.updateByPrimaryKey(returnGoods);
        ReturnProcessExample example = new ReturnProcessExample();
        example.createCriteria().andReturnGoodsIdEqualTo(returnGoodsId);
        List<ReturnProcess> returnProcesses = returnProcessMapper.selectByExample(example);
        for(ReturnProcess returnProcess:returnProcesses){
            returnProcess.setStatus(5);
            returnProcessMapper.updateByPrimaryKey(returnProcess);
        }
    }

    /**
     * 修改returnprocess状态为4，修改returngoods状态为3，将total返回给user
     * @param returnGoodsId
     * @throws Exception
     */
    public void commitReturnMoney(Integer returnGoodsId) throws Exception {
        ReturnGoods returnGoods = returnGoodsMapper.selectByPrimaryKey(returnGoodsId);
        returnGoods.setStatus(3);
        returnGoodsMapper.updateByPrimaryKey(returnGoods);
        ReturnProcessExample example = new ReturnProcessExample();
        example.createCriteria().andReturnGoodsIdEqualTo(returnGoodsId);
        List<ReturnProcess> returnProcesses = returnProcessMapper.selectByExample(example);
        for(ReturnProcess returnProcess:returnProcesses){
            returnProcess.setStatus(4);
            returnProcessMapper.updateByPrimaryKey(returnProcess);
        }
        //找出orderinfoid，通过这个找出总价
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(returnGoods.getOrderId());
        Double money = orderInfo.getMoney();
        User user = userMapper.selectByPrimaryKey(returnGoods.getUserId());
        user.setMoney(user.getMoney().add(BigDecimal.valueOf(money)));
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public Integer selectCountById(Integer id) throws Exception {
        Integer storeId = personMapper.selectByPrimaryKey(id).getStoreId();
        return orderMapper.selectCountById(storeId);
    }

    /**
     * 查询是否有这个person对象
     * @param tel
     * @return
     * @throws Exception
     */
    public Person loginByTel(String tel) throws Exception {
        PersonExample example = new PersonExample();
        example.createCriteria().andTelEqualTo(tel);
        List<Person> persons = personMapper.selectByExample(example);
        if(persons==null){
            throw new IncorrectCredentialsException();
        }
        return persons.get(0);
    }

    @Override
    public Integer selectReturnCountById(Integer userId) throws Exception {
        return returnGoodsMapper.selectCountById(userId);
    }
}
